/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.oai.metadataRepository.cli.alephHarvest;

import cz.mzk.oai.metadataRepository.config.PropertyLoader;
import cz.mzk.oai.metadataRepository.harvest.marc21.AlephExportHarvester;
import cz.mzk.oai.metadataRepository.metadata.DateStamp;
import cz.mzk.oai.metadataRepository.repository.AlephBase;
import cz.mzk.oai.metadataRepository.repository.Repository;
import java.io.File;
import java.io.IOException;
import java.util.Properties;

/**
 *
 * @author Martin Řehánek (rehan at mzk.cz)
 */
public class ExportHarvester implements Harvester {

    private static final String PROP_ALEPH_EXPORT_DIR = "repositoryManager.AlephExportHarvester.exportDir";
    private static final String PROP_HARVEST_LOG = "repositoryManager.harvest.log";
    private final Repository repository;
    private final Properties properties;
    private File exportDir;
    private File harvestLog;

    public ExportHarvester(Repository repository, Properties properties) {
        this.repository = repository;
        this.properties = properties;
    }

    public void harvestAll(AlephBase base) throws IOException {
        harvest(base, null, null);
    }

    public void harvestFrom(AlephBase base, DateStamp from) throws IOException {
        harvest(base, from, null);
    }

    public void harvestBetween(AlephBase base, DateStamp from, DateStamp to) throws IOException {
        harvest(base, from, to);
    }

    private void harvest(AlephBase base, DateStamp from, DateStamp to) throws IOException {
        loadLogFileAndExportDir();
        AlephExportHarvester harvester = new AlephExportHarvester(base, exportDir, repository, harvestLog);
        harvester.harvest(from, to);
    }

    private void loadLogFileAndExportDir() {
        PropertyLoader loader = new PropertyLoader(properties);
        exportDir = loader.loadDir(PROP_ALEPH_EXPORT_DIR);
        harvestLog = loader.loadFile(PROP_HARVEST_LOG, false);
    }
}
